//
//  NSString+Encryption.m
//  Public
//
//  Created by wangcl on 2018/5/15.
//  Copyright © 2018年 wangcl. All rights reserved.
//

#import "NSString+Encryption.h"
#import <CommonCrypto/CommonCrypto.h>

@implementation NSString (Encryption)

- (NSString *)base64EncodedString;
{
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:1];
}

- (NSString *)base64DecodedString
{
    NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:1];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}

-(NSString * )md5String;
{
    const char * cStr = [self UTF8String];
    unsigned char result[32];
    //CC_MD5( cStr, strlen(cStr), result);
    CC_MD5( cStr, (CC_LONG)(strlen(cStr)), result);
    
    NSString * md5Str= [NSString stringWithFormat:
                        @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                        result[0], result[1], result[2], result[3],
                        result[4], result[5], result[6], result[7],
                        result[8], result[9], result[10], result[11],
                        result[12], result[13], result[14], result[15]
                        ];
    return [md5Str  lowercaseString];
}

@end
